<?php
namespace app\zw_admin\controller;
use think\Controller;
use think\Request;
use think\Db;
use think\Session;
use Payment\Common\PayException;
use Payment\Client\Charge;
class OrderController extends Controller
{
	//交易订单号详情
    public function index()
    {
    	$order_list = db('merge_order')->where(['ali_type'=>'1'])->order('merge_id desc')->select();

        $userList = [];
        $orderList = [];
        foreach ($order_list as $key => $value) {
            $orderList[$key] = $value;
            $userList = db('member')->where(['uid'=>$value['uid']])->select();
            
            foreach ($userList as $k => $v) {
                // dump($v['phone']);
                if ($value['uid'] == $v['uid']) {
                    $orderList[$key]['phone'] = $v['phone'];
                }
            //     $orderList[$key]['phone'] = $v['phone'];
            }

        }
    	// $order_list = Db::name('merge_order')
     //    ->alias('a')
     //    ->join('drm_member b','b.uid = a.uid')
     //    ->where(['ali_type'=>'1'])
     //    ->order('merge_id desc')
     //    ->select();
        
        // foreach ($orderList as $key => $value) {
        //     dump($value);
        // }
        // dump($orderList);
        // exit;
        $this->assign('order_list',$orderList);
        return $this->fetch();
    }



    /**
     * [订单详情页]  
     * @return [merge_id] [交易订单号]
     */
    public function list()
    {
    	$mid = input('param.merge_id');
    	// echo $mid;  
    	// exit;
    	$mList = db('merge_order')->where(['merge_id'=>$mid])->find();
    	$numberArr = explode(",",$mList['number']);
    	$numberList = '';
        foreach ($numberArr as $key => $value) {
            $numberList .= "`number`="."'".$value."'"." or ";

        }
        $numberList = rtrim($numberList,'or ');                                                             
     //    echo $numberList;
     //    exit;
    	// dump($numberList);
    	// exit;
    	$order_list = Db::name('goods_order')->where($numberList)->select();
    	// $goods_list = [];
    	$goodsOrder = [];
    	$goodsList = [];
    	$seller_list = [];
    	$goodsOrderList = [];
        foreach ($order_list as $key => $value) {
        	$goodsOrder = $value;
        	$goods_list = db('goods')->where(['number'=>$value['number']])->select();
        	$seller_list = db('seller')->where(['seller_id'=>$value['seller_id']])->select();
        	// dump($seller_list);
        	foreach ($goods_list as $k => $v) {
        		$goodsOrder['goods_name'] = $v['goods_name'];
        		$goodsOrder['goods_id'] = $v['goods_id'];
        		$goodsOrder['com'] = $v['com'];
        		$goodsOrder['num'] = $v['num'];
        		$goodsOrder['brand_name'] = $v['brand_name'];
        		$goodsOrder['unit'] = $v['unit'];
        		$goodsOrder['content'] = $v['content'];
        		$goodsOrder['good_type'] = $v['good_type'];
        		$goodsOrder['g_id'] = $v['g_id'];
        		
        		
        	}
        	foreach ($seller_list as $kk => $vv) {
        		$goodsOrder['seller_name'] = $vv['brand_name'];
        	}
        	$goodsOrderList[] = $goodsOrder;
        	
        }
        // exit;
        // dump($goodsOrderList);
        // exit;
        $this->assign('goodsOrderList',$goodsOrderList);
        return $this->fetch();
    }





    /**
     * [quit_order 退换/货订单详情]
     * @return [type] [description]
     */
    public function quit_order()
    {

    	$quitGoodsList = db('quit_goods')
    	->alias('a')
        ->join('drm_goods b','b.goods_id = a.gid')
        ->order('time desc')
        ->where(['record_type'=>'0'])
        ->select();
        // dump($quitGoodsList);
        // exit;
        $listArr = [];
        $listArrTo = [];
        $userList = [];
        foreach ($quitGoodsList as $key => $value) {
        	$listArr = $value; 
        	$userList = db('member')->where(['uid'=>$value['uid']])->select();
        	foreach ($userList as $k => $v) {
        		$listArr['phone'] = $v['phone'];
        	}
        	$listArrTo[] = $listArr;
        	
        }
        // dump($listArrTo);
        // exit;
        // dump($quitGoodsList);
        // exit;
    	
    	
    	
    	$this->assign('listArrTo',$listArrTo);
        return $this->fetch();
    	// dump($number);
    	// exit;
    }



    /**
     * 退款商品详情
     * @return [type] [description]
     */
    public function quitlist()
    {
    	// echo '111';
    	// exit;
    	$gid = input('param.gid');
    	/**
    	 * 查询商品/订单列表
    	 */
    	$goodsListOne = db('goods')
    	->alias('a')
        ->join('drm_goods_order b','b.number = a.number')
        ->where(['goods_id'=>$gid])
        ->find();
        /**
         * 判断是否是限时特价商品
         */
        if ($goodsListOne['good_type'] == '1') {
        	$activityGoods = db('activity_goods')->where(['g_id'=>$goodsListOne['g_id']])->find();
        	$goodsListOne['initial_unit'] = $activityGoods['unit'];
        	$goodsListOne['live_unit'] = $activityGoods['live_unit'];
        	$goodsListOne['quota'] = $activityGoods['quota'];
        }else{
        	$goodsListOne['initial_unit'] = $goodsListOne['unit'];
        	$goodsListOne['live_unit'] = '0';
        	$goodsListOne['quota'] = '0';
        }
        /**
         * 获取退货申请信息
         */
        $quitList = db('quit_goods')->where(['gid'=>$goodsListOne['goods_id']])->find();
        $goodsListOne['quit_type'] = $quitList['quit_type'];
        $goodsListOne['reason'] = $quitList['reason'];
        $goodsListOne['quit_content'] = $quitList['content'];

        /**
         * 获取交易订单信息
         */
        $mergeList = db('merge_order')->where(['ali_type'=>'1'])->select();
        $numList = [];
    	$number = [];
    	foreach ($mergeList as $key => $value) {
    		$numList = explode(",",$value['number']);
    		foreach ($numList as $k => $v) {
    			if ($v == $goodsListOne['number']) {
    				$number['num'] = $value['merge_number'];
    			}
    		}
    	}
    	$goodsListOne['merge_number'] = $number['num'];
        $recordList = db('record')->where(['merge_number'=>$goodsListOne['merge_number']])->find();
        $goodsListOne['ali_unit'] = $recordList['ali_unit'];
        $goodsListOne['wx_unit'] = $recordList['wx_unit'];
        $goodsListOne['balance_unit'] = $recordList['balance_unit'];
        $goodsListOne['rid'] = $recordList['rid'];
        $goodsListOne['refund_unit'] = $goodsListOne['unit'] * $goodsListOne['num'];
        $goodsArr = [];
    	$list[] = $goodsListOne;
        $refundList = db('goods_refund')->where(['gid'=>$goodsListOne['goods_id']])->find();
        foreach ($list as $key => $value) {
            $goodsArr[$key] = $value;
            $goodsArr[$key]['refund_number'] = $refundList['refund_number'];
            
        }
    	// dump($goodsArr);
    	// exit;
    	$this->assign('list',$goodsArr);
    	return $this->fetch('quitlist');
    	// $goodsList = db('goods')
    }







    /**
     * 退款判断操作
     */
    public function refund()
    {                  
        // echo '111';
        // exit;
        // dump(input('param.'));
        // exit;
        $gid = input('param.goods_id');
        $refundnumber = input('param.refundnumber');

        /**
         * 查询商品订单号
         */
        $goodsList = db("goods")->where(['goods_id'=>$gid])->find();
        
        /**
         * 根据商品订单号 获得交易订单号
         */
        $mergeList = db('merge_order')->where(['ali_type'=>'1'])->select();
        $numList = [];
        $number = [];
        foreach ($mergeList as $key => $value) {
            $numList = explode(",",$value['number']);
            foreach ($numList as $k => $v) {
                if ($v == $goodsList['number']) {
                    $number = $value['merge_number'];
                }
            }
        }
      
        /**
         * 根据交易订单号查询退款信息
         */
        $refundList = db('record')->where(['merge_number'=>$number])->find();
        // dump($refundList);
        // exit;
        /**
         * 根据支付方式 进行退款操作
         */
        switch ($refundList['type']) {
            case '1':  //支付宝退款
                //获取商品交易流水号
                $out_request_no = $goodsList['out_request_no'];
                $this->ali_refund($gid,$goodsList['unit'],$number,$goodsList['uid'],$out_request_no,$refundnumber);
                break; 
            case '3':  //余额退款
                $this->balance_refund($gid,$goodsList['unit'],$number,$goodsList['uid'],$refundnumber);
                break;      
            case '4':  //支付宝&余额组合退款
                $goodsRefund = db('goods_refund')->where(['merge_number'=>$number,'gid'=>$gid])->find();
                //余额退款金额
                $balance_unit = $goodsRefund['balance_unit'];
                //支付宝退款金额
                $ali_unit = $goodsRefund['ali_unit'];
                // dump($goodsRefund);
                // exit;
                //判断是否部分商品已退款
                // if (empty($goodsRefund)) {
                //     //如果商品金额大于支付支付金额
                //     if ($goodsList['unit'] >= $refundList['ali_unit']) {
                //         //获取余额退款金额
                //         $balance_unit = $goodsList['unit'] - $refundList['ali_unit'];
                //         $balance_unit = sprintf("%.2f",$balance_unit);
                //         //获取支付宝退款金额
                //         $ali_unit = $goodsList['unit'] - $balance_unit;
                //         $ali_unit = sprintf("%.2f",$ali_unit);
                //     }else{
                //     //如果支付宝支付金额大于商品金额
                //         //获取支付宝退款金额
                //         $ali_count = $refundList['ali_unit'] - $goodsList['unit'];
                //         $ali_unit = $refundList['ali_unit'] - $ali_count;
                //         $ali_unit = sprintf("%.2f",$ali_unit);
                //         //余额退款金额
                //         $balance_unit = '0';
                        
                //     }
                // }else{
                //     if ($goodsRefund['ali_unit'] >= $refundList['ali_unit']) {
                //         //获取支付宝退款金额
                //         $ali_unit = '0';
                //         //余额退款金额
                //         $balance_unit = $goodsList['unit'];
                //     }else{
                //         //获取支付宝退款金额
                //         $ali_count = $refundList['ali_unit'] - $goodsRefund['ali_unit'];
                //         $balance_unit = $goodsList['unit'] - $ali_count;
                //         $balance_unit = sprintf("%.2f",$balance_unit);
                //         //获取支付宝退款金额
                //         $ali_unit = $goodsList['unit'] - $balance_unit;
                //         $ali_unit = sprintf("%.2f",$ali_unit);
                        
                //     }
                // }
                //获取商品交易流水号
                $out_request_no = $goodsList['out_request_no'];
                
                $this->ali_balance_refund($gid,$balance_unit,$ali_unit,$number,$goodsList['uid'],$out_request_no,$refundnumber);
                break;   
        }
       
    }




    /**
     * 余额退款
     *gid  商品id
     *unit  商品单价
     *merge_order  交易订单号
     *uid  用户id
     */
    public function balance_refund($gid,$unit,$merge_number,$uid,$refundnumber)
    {
       
        /**
         * 退款记录
         */
        
        $goodsRefund['type'] = '1';

        db('goods_refund')->where(['refund_number'=>$refundnumber])->update($goodsRefund);
        // dump($goodsRefund);
        // exit;
        /**
         * 修改订单状态 goods     type:6
         * 返回退款状态  merge_order / record  record_type: 1
         */
        $type['type'] = '6';
        $record_type['record_type'] = '1';
        db('goods')->where(['goods_id'=>$gid])->update($type);
        db('quit_goods')->where(['gid'=>$gid])->update($record_type);
        $uList = db('account')->where(['uid'=>$uid])->find();
        /**
         * 获取退款金额
         */
        $balance_unit['balance'] = $unit + $uList['balance'];
        /**
         * 将款项退至用户账户
         */
        if (db('account')->where(['uid'=>$uid])->update($balance_unit) > 0) {
            echo '111';
            exit;
        }else{
            echo '222';
            exit;
        }


    }







    /**
     * 支付宝退款操作
     */
    public function ali_refund($gid,$unit,$merge_number,$uid,$out_request_no,$refundnumber)
    {
        

        $out_trade_no = $merge_number;
        $refund_amount = $unit;
        // $reason = $quitList['content'];
        // $refund_order = 'HZ01RF001';
        include_once ROOT_PATH.'extend/alipay/AopSdk.php';
        $aop = new \AopClient ();
        $aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
        $aop->appId = '2018071360723002';
        $aop->rsaPrivateKey = 'MIIEpAIBAAKCAQEAwK99IOj0b1d/XjJsDRQ4yWIjfXGOIhds1fXK6lrAzCxY5YUG7EWzA660taBS+xaGG7MozeZEIIhua+AFs+FJ6WfcQkmmXQlT3iZO2RMbikb7fMAvgUf916CnpcHcTaUo/G8sgI8zoM06zNbAMrqIAhwJEEm5oN6jBHT5v9elbtoLtPgIpUrq8pNhOWBus8JUN9PojcDcL95JfnvJVJt1gmAl9X5ai5xqqLtgY6f0TnFfOpPIA9NBH1c24Qy2JJ8RHb/NOZlun4grTMb/COhVlXo9L/90YV1v3KIN4WZ8d2SuzOJZTukk0Xdg9CxmsbBYy+YHUDqUbOIW+H1nuz4cnwIDAQABAoIBAFHvTaoc5TQjZMnNbxzwqGUaEjVZgDMG+wTQjASzqqAni3Wt54+447KBK96/2gljPouw130WmYmr+ihtDn+7IQ7bV1Hot5GAUeV5LLYp5eAa5v3ZS1oSHuL+McvOb7ukq5UMQUQ5oJ6UJpjMCNaJcyRSf5gpgYHiqjdO9pSZQoOr/nsXA3+llzoZJyVR6GlwcrcHgHNKGvycd5q0CDM/es3grt1Gsj9uJZmExLcoNonH5ech27raEYE63v7tXnNgb/FqIv9f3TpYbxutfDUNBM+2bS9TlFWdpwjeFLoVUzWGYQqdhlHBpUjIcVRGogJ6RTpFQvAvj7tBHzFueROHoGECgYEA+Yla5elBKJ6hV3veNf1nfULtWLvofBXACzfY5m3J8029QMDh6UE0IWZ50S/crmcJptGMSMcJCWIaNh/OAiqMslFnXBMIc9IhWI8QB98Ij5LGwZ1cnoZHIMZiq7/vP9bopFIQe/ftMwOjh42Bk/L39ecmU7ZBmfGYECMwisjp2JUCgYEAxa0pZypV54QwDTVSxK2/qmPiyCkNtS180nAf1AbTb+5ZNyoysiAcU1R1rfVR0QA437L/m3zRv0KLYJGCoZkAqmbWb4Ue6zPLMj1JZNwcdWLVBbdye4bG0wOIyNSdJTsqOUiHju7scnYjkNLhcQfgVJhMDhcaFg0MIE9/ZpLcL2MCgYBsuZ9Ea7R7SVrNLmhsp8POvTmDHyS15BgwCx5TkGScwTODokcMhd976hWLwsnpp3V6HPV9xFM4pmj7SjJNmVKCY5ugb56QMG9dEcCPXWtyeyZkt/biD2mwayAU62UIMNBewG70RNsyDhTTfCGIYCYQG64EbXX56F00MtD0t4PEXQKBgQCrxpvx8FISC0aOU8YG701czcvmT/Z/eHW9kH24C+50l9SjIWJIc6rppr1UW5erXNwPWEbSYRcLctIXKpsvvftcGF1DL5T8oldy4tsCcFuyjh27B/zN5wKjn7CfhFLCZbV9bc+On4SMuqiRUw9g341LNcTxgZXDj7b35N06z2fTLQKBgQCnoUX2c/9qnsvyY9wBA7rbunUJZEbWZnZzIWg1dz1JefqiITCn8/+mRj1TUbh7wzqR2cygNxBg9JrkHhgZS3FG8nuUHkQViwtizj2STjsRpuGOh6H+NawaeV9uM9oq/225ub3869gsnmPlJnw1zqW3m+roCAxw707P1chVuEnB5Q==';
        $aop->alipayrsaPublicKey='MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtkUtWxCXkAvcJGPIRtKhJSFdzxWblawi91kASYkkWqgqgl80CJqxjtEaQnN8BAq9CKoHL3wwMuVeDh6WRTVN4P+/Ie1I4F509NRyUXXg9EykET7M8uuOQyO9tud8kyWcmBzIR3Ltp6j+MwMwkE9+3NxChABM1n+AnfDMEoDrWJWgTDb8X/AlidtZ/kmXDLYNM7HbRwZ/cLMmbRFh8p7E5wosuZDR7S4tgTZaxFwnycK/e9IWV+Zw2Fl6zWFaAaIBqPzo/K+0sc70gdHwaRQw+TGrg8gE62Uid0p2q5reJ5QMUzCrKpNuCndKCv0QANTR74el0aTYv0OrmUbGTTJpgQIDAQAB';
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset='utf-8';
        $aop->format='json';
        $request = new \AlipayTradeRefundRequest();
        $request->setBizContent("{" .
//            "\"trade_no\":\"2017112821001004030523090753\"," .
            "\"out_trade_no\":\"$out_trade_no\"," .
            "\"refund_amount\":$refund_amount," .
            "\"refund_reason\":\"正常退款\"," .
            "\"out_request_no\":\"$out_request_no\"," . //流水号
            "\"operator_id\":\"OP001\"," .
            "\"store_id\":\"NJ_S_001\"," .
            "\"terminal_id\":\"NJ_T_001\"" .
            "  }");

        $result = $aop->execute($request);
        // dump($result);
        // exit;
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        $resultCode = $result->$responseNode->code;
       
        if(!empty($resultCode)&&$resultCode == 10000){
            /**
             * 退款记录
             */
            $goodsRefund['type'] = '1';

            db('goods_refund')->where(['refund_number'=>$refundnumber])->update($goodsRefund);
            /**
             * 修改订单状态 goods     type:6
             * 返回退款状态  merge_order / record  record_type: 1
             */
            $alitype['type'] = '6';
            db('goods')->where(['goods_id'=>$gid])->update($alitype);
            $record_type['record_type'] = '1';
            // db('goods')->where(['goods_id'=>$gid])->update($type);
            db('quit_goods')->where(['gid'=>$gid])->update($record_type);
            echo '111';
            exit;
        } else {
//            echo "失败";
            throw new Exception($result->$responseNode->sub_msg);
        }
    }




    /**
     * 支付宝/余额组合退款操作
     */
    public function ali_balance_refund($gid,$balance_unit,$ali_unit,$merge_number,$uid,$out_request_no,$refundnumber)
    {
        // echo $ali_unit;
        // echo $balance_unit;
        // exit;
        if ($ali_unit != '0') {
            $out_trade_no = $merge_number;
            $refund_amount = $ali_unit;
            // $reason = $quitList['content'];
            // $refund_order = 'HZ01RF001';
            include_once ROOT_PATH.'extend/alipay/AopSdk.php';
            $aop = new \AopClient ();
            $aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
            $aop->appId = '2018071360723002';
            $aop->rsaPrivateKey = 'MIIEpAIBAAKCAQEAwK99IOj0b1d/XjJsDRQ4yWIjfXGOIhds1fXK6lrAzCxY5YUG7EWzA660taBS+xaGG7MozeZEIIhua+AFs+FJ6WfcQkmmXQlT3iZO2RMbikb7fMAvgUf916CnpcHcTaUo/G8sgI8zoM06zNbAMrqIAhwJEEm5oN6jBHT5v9elbtoLtPgIpUrq8pNhOWBus8JUN9PojcDcL95JfnvJVJt1gmAl9X5ai5xqqLtgY6f0TnFfOpPIA9NBH1c24Qy2JJ8RHb/NOZlun4grTMb/COhVlXo9L/90YV1v3KIN4WZ8d2SuzOJZTukk0Xdg9CxmsbBYy+YHUDqUbOIW+H1nuz4cnwIDAQABAoIBAFHvTaoc5TQjZMnNbxzwqGUaEjVZgDMG+wTQjASzqqAni3Wt54+447KBK96/2gljPouw130WmYmr+ihtDn+7IQ7bV1Hot5GAUeV5LLYp5eAa5v3ZS1oSHuL+McvOb7ukq5UMQUQ5oJ6UJpjMCNaJcyRSf5gpgYHiqjdO9pSZQoOr/nsXA3+llzoZJyVR6GlwcrcHgHNKGvycd5q0CDM/es3grt1Gsj9uJZmExLcoNonH5ech27raEYE63v7tXnNgb/FqIv9f3TpYbxutfDUNBM+2bS9TlFWdpwjeFLoVUzWGYQqdhlHBpUjIcVRGogJ6RTpFQvAvj7tBHzFueROHoGECgYEA+Yla5elBKJ6hV3veNf1nfULtWLvofBXACzfY5m3J8029QMDh6UE0IWZ50S/crmcJptGMSMcJCWIaNh/OAiqMslFnXBMIc9IhWI8QB98Ij5LGwZ1cnoZHIMZiq7/vP9bopFIQe/ftMwOjh42Bk/L39ecmU7ZBmfGYECMwisjp2JUCgYEAxa0pZypV54QwDTVSxK2/qmPiyCkNtS180nAf1AbTb+5ZNyoysiAcU1R1rfVR0QA437L/m3zRv0KLYJGCoZkAqmbWb4Ue6zPLMj1JZNwcdWLVBbdye4bG0wOIyNSdJTsqOUiHju7scnYjkNLhcQfgVJhMDhcaFg0MIE9/ZpLcL2MCgYBsuZ9Ea7R7SVrNLmhsp8POvTmDHyS15BgwCx5TkGScwTODokcMhd976hWLwsnpp3V6HPV9xFM4pmj7SjJNmVKCY5ugb56QMG9dEcCPXWtyeyZkt/biD2mwayAU62UIMNBewG70RNsyDhTTfCGIYCYQG64EbXX56F00MtD0t4PEXQKBgQCrxpvx8FISC0aOU8YG701czcvmT/Z/eHW9kH24C+50l9SjIWJIc6rppr1UW5erXNwPWEbSYRcLctIXKpsvvftcGF1DL5T8oldy4tsCcFuyjh27B/zN5wKjn7CfhFLCZbV9bc+On4SMuqiRUw9g341LNcTxgZXDj7b35N06z2fTLQKBgQCnoUX2c/9qnsvyY9wBA7rbunUJZEbWZnZzIWg1dz1JefqiITCn8/+mRj1TUbh7wzqR2cygNxBg9JrkHhgZS3FG8nuUHkQViwtizj2STjsRpuGOh6H+NawaeV9uM9oq/225ub3869gsnmPlJnw1zqW3m+roCAxw707P1chVuEnB5Q==';
            $aop->alipayrsaPublicKey='MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtkUtWxCXkAvcJGPIRtKhJSFdzxWblawi91kASYkkWqgqgl80CJqxjtEaQnN8BAq9CKoHL3wwMuVeDh6WRTVN4P+/Ie1I4F509NRyUXXg9EykET7M8uuOQyO9tud8kyWcmBzIR3Ltp6j+MwMwkE9+3NxChABM1n+AnfDMEoDrWJWgTDb8X/AlidtZ/kmXDLYNM7HbRwZ/cLMmbRFh8p7E5wosuZDR7S4tgTZaxFwnycK/e9IWV+Zw2Fl6zWFaAaIBqPzo/K+0sc70gdHwaRQw+TGrg8gE62Uid0p2q5reJ5QMUzCrKpNuCndKCv0QANTR74el0aTYv0OrmUbGTTJpgQIDAQAB';
            $aop->apiVersion = '1.0';
            $aop->signType = 'RSA2';
            $aop->postCharset='utf-8';
            $aop->format='json';
            $request = new \AlipayTradeRefundRequest();
            $request->setBizContent("{" .
    //            "\"trade_no\":\"2017112821001004030523090753\"," .
                "\"out_trade_no\":\"$out_trade_no\"," .
                "\"refund_amount\":$refund_amount," .
                "\"refund_reason\":\"正常退款\"," .
                "\"out_request_no\":\"$out_request_no\"," .
                "\"operator_id\":\"OP001\"," .
                "\"store_id\":\"NJ_S_001\"," .
                "\"terminal_id\":\"NJ_T_001\"" .
                "  }");

            $result = $aop->execute($request);
            // dump($result);
            // exit;
            $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
            $resultCode = $result->$responseNode->code;
           
            if(!empty($resultCode)&&$resultCode == 10000){
                /**
                 * 退款记录
                 */
                $goodsRefund['type'] = '1';

                db('goods_refund')->where(['refund_number'=>$refundnumber])->update($goodsRefund);
                /**
                 * 修改订单状态 goods     type:6
                 * 返回退款状态  merge_order / record  record_type: 1
                 */
                // $type['type'] = '6';
                $alitype['type'] = '6';
                db('goods')->where(['goods_id'=>$gid])->update($alitype);
                $record_type['record_type'] = '1';
                db('quit_goods')->where(['gid'=>$gid])->update($record_type);
                $uList = db('account')->where(['uid'=>$uid])->find();
                /**
                 * 获取退款金额
                 */
                $balancelist['balance'] = $balance_unit + $uList['balance'];
                /**
                 * 将款项退至用户账户
                 */
                if (db('account')->where(['uid'=>$uid])->update($balancelist) > 0) {
                    echo '111';
                    exit;
                }else{
                    echo '222';
                    exit;
                }
            } else {
    //            echo "失败";
                throw new Exception($result->$responseNode->sub_msg);
            }
        }else{
            /**
             * 退款记录
             */
            $goodsRefund['type'] = '1';

            db('goods_refund')->where(['refund_number'=>$refundnumber])->update($goodsRefund);
            // dump($goodsRefund);
            // exit;
            /**
             * 修改订单状态 goods     type:6
             * 返回退款状态  merge_order / record  record_type: 1
             */
            $type['type'] = '6';
            $record_type['record_type'] = '1';
            db('goods')->where(['goods_id'=>$gid])->update($type);
            db('quit_goods')->where(['gid'=>$gid])->update($record_type);
            $uList = db('account')->where(['uid'=>$uid])->find();
            /**
             * 获取退款金额
             */
            $balancelist['balance'] = $balance_unit + $uList['balance'];
            /**
             * 将款项退至用户账户
             */
            if (db('account')->where(['uid'=>$uid])->update($balancelist) > 0) {
                echo '111';
                exit;
            }else{
                echo '222';
                exit;
            }

        }
    }
    




    // public function 










}